﻿using DevExpress.XtraEditors;
using DXApplicationTangche.UC.抖音出入库;
using DXApplicationTangche.UC.抖音出入库.Service;
using DXApplicationTangche.UC.报表.service;
using DXApplicationTangche.UC.有赞.Service;
using DXApplicationTangche.UC.有赞.有赞商品;
using DXApplicationTangche.UC.面料审核.Service;
using DXApplicationTangche.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.报表
{
    public partial class Form抖音有赞商品对照 : DevExpress.XtraEditors.XtraForm
    {
        // 抖音SKU
        private List<DouyinSkuDao> douyinSkuDaos = new List<DouyinSkuDao>();
        public Form抖音有赞商品对照()
        {
            InitializeComponent();
        }

        #region 抖音SKU
        private void 刷新抖音SKU()
        {
            this.douyinSkuDaos = 抖音Service.getDouyinSKU();
            this.gridControl抖音sku一览.DataSource = this.douyinSkuDaos;
            this.gridView抖音sku一览.RefreshData();
            this.gridView抖音sku一览.ExpandAllGroups();
        }

        private void 保存成衣参考码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String skuId = this.gridView抖音sku一览.GetRowCellValue(this.gridView抖音sku一览.FocusedRowHandle, "skuId").ToString();
                foreach (DouyinSkuDao item in this.douyinSkuDaos)
                {
                    if (skuId.Equals(item.skuId))
                    {
                        new Form抖音保存成衣参考码(item).ShowDialog();
                        break;
                    }
                }
                this.刷新抖音SKU();
            }
            catch
            {
                return;
            }
        }

        private void gridView抖音sku一览_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name == "gridColumn成衣条码")
            {
                if (e.CellValue == null)
                {
                    e.Appearance.BackColor = Color.OrangeRed;
                }
                e.Appearance.BackColor = Color.LightGreen;
            }
        }

        /// <summary>
        /// csv导入抖音sku
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButtonCSV导入抖音sku_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "csv|*.csv";
            ofd.Title = "请选择csv文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //  csv导入
                String re = 抖音Service.saveDouyinSku(ofd.FileName);
                MessageBox.Show("csv上传成功");
                this.刷新抖音SKU();
            }
        }

        private void simpleButton刷新抖音SKU_Click(object sender, EventArgs e)
        {
            this.刷新抖音SKU();
        }

        #endregion

        #region 有赞SKU
        private void 刷新有赞SKU()
        {
            this.gridControl有赞SKU一览.DataSource = 有赞商品Service.getViewSpuCostList();
            this.gridView有赞SKU一览.RefreshData();
        }

        private void 更新有赞SKU信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String itemId = this.gridView有赞SKU一览.GetRowCellValue(this.gridView有赞SKU一览.FocusedRowHandle, "itemId").ToString();
                String title = this.gridView有赞SKU一览.GetRowCellValue(this.gridView有赞SKU一览.FocusedRowHandle, "title").ToString();
                String detailUrl = this.gridView有赞SKU一览.GetRowCellValue(this.gridView有赞SKU一览.FocusedRowHandle, "detailUrl").ToString();
                new Form更新有赞SKU(itemId, title, detailUrl).ShowDialog();
                this.刷新有赞SKU();
            }
            catch
            {
                return;
            }
        }
        private void simpleButton刷新有赞SKU_Click(object sender, EventArgs e)
        {
            this.刷新有赞SKU();
        }

        private void simpleButton同步有赞款式到数据库_Click(object sender, EventArgs e)
        {
            //  同步有赞款式到数据库——春衫定制
            有赞商品Service.saveYouzanSpuItemsAndSkusByGroup("2oluqykobnvyk", 216892094, "春衫定制");
            this.刷新有赞SKU();
        }
        #endregion

        #region 抖音有赞商品对照
        private  void 刷新对照()
        {
            //  抖音和有赞商品对照表
            this.gridControl抖音和有赞商品对照表.DataSource = 抖音报表service.get抖音和有赞商品对照表();
            this.gridView抖音和有赞商品对照表.RefreshData();
        }

        private void simpleButton刷新对照_Click(object sender, EventArgs e)
        {
            this.刷新对照();
        }

        #endregion

        private void simpleButton导入抖音订单_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            //ofd.Filter = "csv|*.csv";
            ofd.Title = "请选择csv文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //  csv导入
                String re = 面料审核Service.saveDouyinOrder("SSHIRT", "11", ofd.FileName);
                MessageBox.Show("订单csv上传成功");
            }
        }

        private void simpleButton导入抖音售后单_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = MIMEConst.EXCEL_MIME;
            openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFile.Multiselect = false;
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                ExcelAndPath excelAndPath = ExcelUtil.getExcelDataAndPath(openFile.FileName);
                if (excelAndPath == null)
                {
                    return;
                }
                DataSet result = excelAndPath.DataSet;
                if (result == null)
                {
                    return;
                }
                DataTable dataTable = result.Tables[0];
                //获取数据
                List<DouyinOrderReturnDTO> douyinOrderReturnDTOs = new List<DouyinOrderReturnDTO>();
                for (int i = 1; i < dataTable.Rows.Count; i++)
                {
                    try
                    {
                        if (String.IsNullOrEmpty(dataTable.Rows[i]["Column0"].ToString()))
                        {
                            break;
                        }
                    }
                    catch (Exception)
                    {
                        break;
                    }
                    #region
                    douyinOrderReturnDTOs.Add(
                        new DouyinOrderReturnDTO(
                         dataTable.Rows[i]["Column0"].ToString(),    //售后单号
                         dataTable.Rows[i]["Column1"].ToString(),  //订单号
                         dataTable.Rows[i]["Column2"].ToString(),  //商品单号
                         dataTable.Rows[i]["Column3"].ToString(), //商品名称
                         dataTable.Rows[i]["Column4"].ToString(),   //商品ID
                         dataTable.Rows[i]["Column5"].ToString(), //应付金额（元）
                         dataTable.Rows[i]["Column6"].ToString(),   //商品运费（元）
                         dataTable.Rows[i]["Column7"].ToString(),//支付优惠（元）
                         dataTable.Rows[i]["Column8"].ToString(),   //商品税费（元）
                         dataTable.Rows[i]["Column9"].ToString(),  //商品发货状态
                         dataTable.Rows[i]["Column10"].ToString(),   //售后类型
                         dataTable.Rows[i]["Column11"].ToString(), //退商品金额（元）
                         dataTable.Rows[i]["Column12"].ToString(), //退运费金额（元）
                         dataTable.Rows[i]["Column13"].ToString(), //退支付优惠（元）
                         dataTable.Rows[i]["Column14"].ToString(),   //退税费金额（元）
                         dataTable.Rows[i]["Column15"].ToString(), //售后状态
                         dataTable.Rows[i]["Column16"].ToString(),    //售后申请时间
                         dataTable.Rows[i]["Column17"].ToString(),  //退款方式
                         dataTable.Rows[i]["Column18"].ToString(),  //是否已上传退款凭证
                         dataTable.Rows[i]["Column19"].ToString(), //售后原因
                         dataTable.Rows[i]["Column20"].ToString(),  //退货物流单号
                         dataTable.Rows[i]["Column21"].ToString(), //退货异常
                         dataTable.Rows[i]["Column22"].ToString(), //退货发货时间
                         dataTable.Rows[i]["Column23"].ToString(),  //退货物流公司
                         dataTable.Rows[i]["Column24"].ToString(),    //自动处理截止时间
                         dataTable.Rows[i]["Column25"].ToString(),  //同意售后申请时间
                         dataTable.Rows[i]["Column26"].ToString(),    //退款成功时间
                         dataTable.Rows[i]["Column27"].ToString(),    //售后关闭时间
                         dataTable.Rows[i]["Column28"].ToString(),  //商家退货地址
                         dataTable.Rows[i]["Column29"].ToString(),    //商家退货联系人姓名
                         dataTable.Rows[i]["Column30"].ToString(),  //商家退货联系人电话
                         dataTable.Rows[i]["Column31"].ToString(),  //客服仲裁
                         dataTable.Rows[i]["Column32"].ToString(),  //纠纷责任方
                         dataTable.Rows[i]["Column33"].ToString(),    //是否极速退款
                         dataTable.Rows[i]["Column34"].ToString(), //正向物流状态
                         dataTable.Rows[i]["Column35"].ToString()   //是否拒签后退款
                        )
                    );
                    #endregion
                }
                抖音订单Service.saveDouyinOrderReturn(douyinOrderReturnDTOs);
                MessageBox.Show("售后单导入成功");
            }
        }

        #region 刷新订单无成衣码
        
        private void dateTimePicker选择日期_ValueChanged(object sender, EventArgs e)
        {
            this.gridControl订单无成衣码.DataSource = 抖音报表service.get对照表(this.dateTimePicker选择日期.Value.ToString());
            this.gridView订单无成衣码.RefreshData();
        }

        #endregion
    }
}